package com.edu;

import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.time.temporal.IsoFields;

/**
 * 
 * 算法可视化器（控制层）
 * 
 * @author Administrator
 *
 */
public class AlgoVisualizer {

	private Object data;
	//private Circle[] circles;
	private AlgoFrame algoFrame; //数据
	//private boolean isAnimated = true;
	
	public AlgoVisualizer() {
	}

	public AlgoVisualizer(int screenWidth, int screenHeight, int n) {
		
		// TODO初始化数据
//		circles = new Circle[n];
//		int r = 50;
//		for (int i = 0; i < circles.length; i++) {
//			int x = (int) (Math.random() * (screenWidth - 2 * r)) + r;
//			int y = (int) (Math.random() * (screenHeight - 2 * r)) + r;
//			int vx = (int) (Math.random() * 11) - 5;
//			int vy = (int) (Math.random() * 11) - 5;
//			circles[i] = new Circle(x, y, r, vx, vy);
//		}

		EventQueue.invokeLater(() -> {
			algoFrame = new AlgoFrame("welcome", screenWidth, screenWidth);
			//TODO 监听事件
			algoFrame.addKeyListener(new AlgoKeyListener());
			algoFrame.addMouseListener(new AlgoMouseListener());
			new Thread(() -> {
				run();
			}).start();
		});
	}

	private void run() {
		//TODO 处理动画的逻辑
//		while (true) {
//			algoFrame.render(circles);
//			AlgoVisHelper.pause(20);
//			if (isAnimated) {
//				for (Circle circle : circles) {
//					circle.move(0, 0, algoFrame.getCanvasWidth(), algoFrame.getCanvasHeight());
//				}
//			}
//		}
	}

	//TODO 键盘监听逻辑
	private class AlgoKeyListener extends KeyAdapter {

//		@Override
//		public void keyReleased(KeyEvent e) {
//			super.keyReleased(e);
//			
//			if(e.getKeyChar()==' ') {
//				isAnimated = !isAnimated;
//			}	
//		}
	}
	//鼠标监听逻辑
	private class AlgoMouseListener extends MouseAdapter {

//		@Override
//		public void mousePressed(MouseEvent e) {
//			e.translatePoint(0,-algoFrame.getInsets().top);
//			//System.out.println(e.getPoint());
//			
//			for (Circle circle : circles) {
//				if(circle.contain(e.getPoint())) {
//					circle.isfilled = ! circle.isfilled;
//				};
//			}
//		}	
	}
}
